package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.entity.OrderState;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王小波
 * @description:  越秀进件同步风险审查
 * @date 2023/8/16 11:20
 */

@Slf4j
@Component
public class LeaseSyncProjectHandler implements StageHandler{
    @Autowired
    private BtoOrderStateService btoOrderStateService;

    @Autowired
    @Lazy
    private YeZhuStageHandler yeZhuStageHandler;

    @Autowired
    @Lazy
    private KanChaStageHandler kanChaStageHandler;


    @Autowired
    @Lazy
    private LeaseSignedHandler leaseSignedHandler;

    private String stageId = BizStageEnum.LEASE_SYNC_PROJECT.getStageId();


    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId , BizStageEnum.LEASE_SYNC_PROJECT.getStageId() , null ,
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId());
    }

    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        OrderState orderTaskState = btoOrderStateService.getOrderTaskState(orderId, this.stageId, null);
        if(orderTaskState.getStateId().equals(BizTaskStateEnum.APPROVAL_PASS_LEASE.getStateId())){
            log.warn("订单 {} 已经进件已经通过，不能重新进件" , orderId);
            return;
        }

        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.LEASE_SYNC_PROJECT.getStageId() , null  ,
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId());
    }

    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId, String approvalType) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.LEASE_SYNC_PROJECT.getStageId() , null , BizTaskStateEnum.APPROVAL_PASS_LEASE.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId() ,true);
        val signedOrderState = btoOrderStateService.getOrderState(orderId, BizStageEnum.LEASE_SIGN.getStageId(), null, BizTaskStateEnum.SIGNED.getStateId());
        //之前签约过，无需重复签约
        if(NullUtils.isNull(signedOrderState)){
            //越秀进件同步审核通过，进入越秀签约
            leaseSignedHandler.createNewInitState(orderId , BizStageEnum.LEASE_SIGN.getStageId() , null );
        }
    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId, String approvalType  , Object rejectInfo) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.LEASE_SYNC_PROJECT.getStageId() , null , BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId()  , true);
        //TODO 如果越秀审核不通过，则需要根据相关的原因，回退相关信息，重新走流程
        yeZhuStageHandler.updateToApprovalRejectState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId(), null , approvalType  , rejectInfo);
        kanChaStageHandler.updateToApprovalRejectState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId(), null , approvalType  , rejectInfo);
    }

    /**
    * 越秀撤单
    */
    public void updateToCloseState(String orderId, String stageId, String taskId, String approvalType  , Object rejectInfo) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.LEASE_SYNC_PROJECT.getStageId() , null ,
                BizTaskStateEnum.APPROVAL_CLOSE_LEASE.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId()  , true);
    }

}
